<template>
  <div v-loading="pageLoading" class="app-rule-form padding-top-20">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" size="small" label-width="110px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="项目名称：" prop="filed01">
            <el-input v-model="ruleForm.filed01" clearable placeholder="请填写" style="width:100%"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目类型：" prop="filed02">
            <el-select v-model="ruleForm.filed02" placeholder="请选择" clearable style="width:100%">
              <el-option v-for="item in mockSelectList" :key="item.value" :label="item.label" :value="item.label" clearable></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="预算金额：" prop="filed03" class="app-input-number-left">
            <el-input-number
              v-model="ruleForm.filed03"
              :controls="false"
              :max="100000000000"
              placeholder="请填写"
              style="width:100%"
            ></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注：" prop="filed09">
            <el-input v-model="ruleForm.filed09" type="textarea" rows="4" placeholder="请填写" style="width:100%"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="">
            <el-button :loading="loading" type="primary" size="small" class="fa fa-check" @click="validateSubmit(ruleForm)">
              保存
            </el-button>
            <el-button size="small" class="fa fa-reply-all" @click="close">
              关闭
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { add, update, getData } from '@/mock/index'

// 导入验证规则
import rules from './rules'
export default {
  name: 'RuleForm',
  props: {
    // 是否是编辑
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 提交表单loading
      loading: false,
      // 页面加载完成loading
      pageLoading: false,
      // 表单
      ruleForm: {
      },
      // 验证
      rules: {
        ...rules
      }
    }
  },
  created () {
    this.initForm()
  },
  methods: {
    initForm () {
      // 用户uuid
      const { uuid } = this.$route.query
      if (uuid) {
        this.uuid = uuid
      }
      // 如果是编辑
      if (this.isEdit) {
        this.getDetail(uuid)
      } else {
        this.pageLoading = false
      }
    },
    // 获取编辑数据
    async getDetail (uuid) {
      // 模拟详情接口
      const detailData = {
        filed01: '',
        filed02: '',
        filed03: ''
      }
      this.pageLoading = true
      const res = await getData({ userUuid: uuid }, detailData)
      this.pageLoading = false
      if (res.code === '0000') {
        this.ruleForm = res.data
      }
    },
    // 关闭跳转
    close () {
      this.closeView(this.$route)
    },
    // 验证
    validateSubmit (ruleForm) {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          this.submit(ruleForm)
        } else {
          return false
        }
      })
    },
    // 提交表单
    async submit (ruleForm) {
      this.loading = true
      if (this.isEdit) {
        // 编辑
        const res = await update(ruleForm)
        if (res.code === '0000') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.jumpRouterView(this.$route, { path: '/user' })
        }
      } else {
        // 增加
        const res = await add(ruleForm)
        if (res.code === '0000') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.jumpRouterView(this.$route, { path: '/user' })
        }
      }
      this.loading = false
    }
  }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
</style>
